package Utils.web.servlet;

import Utils.data.DataConverters;
import Utils.data.DataProcessUtils;
import Utils.db.pojo.Page;
import Utils.db.service.PageService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;

/**
 * @author d11yu
 */
public class ServletUtils {
    /**
     * page方法，无条件获取page对象并返回给指定页面。
     * 注意：需要通过request传入指定页码（"pn"）、每页条目数量（"ecp"，如果没有则为Page.ENTRY_COUNT_PER_PAGE），
     * 会将page对象通过request返回，变量名为"page"。
     *
     * @param request
     * @param response
     * @param pageService
     * @param urlAction   当前servlet url及方法名，例："ClientBookServlet?action=page"
     * @param url         返回的目标网址，例："/pages/client/index.jsp"
     * @param <T>
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     */
    public static <T> void page(HttpServletRequest request, HttpServletResponse response, String urlAction, String url, PageService<T> pageService) throws ServletException, IOException, SQLException {
        int pn = DataConverters.parseInt(request.getParameter("pn"), 1);
        int ecp = DataConverters.parseInt(request.getParameter("ecp"), Page.ENTRY_COUNT_PER_PAGE);
        Page<T> page = pageService.getPage(pn, ecp, urlAction);
        request.setAttribute("page", page);
        request.getRequestDispatcher(url).forward(request, response);
    }

    /**
     * page方法，有条件获取page对象并返回给指定页面。
     * 注意：需要通过request传入指定页码（"pn"）、每页条目数量（"ecp"，如果没有则为Page.ENTRY_COUNT_PER_PAGE），
     * 会将page对象通过request返回，变量名为"page"。
     *
     * @param request
     * @param response
     * @param urlAction   当前servlet url及方法名，例："ClientBookServlet?action=page&min=0&max=100"
     * @param url         返回的目标网址，例："/pages/client/index.jsp"
     * @param pageService pageService
     * @param getPage     pageService.getPageConditional(pn, ecp, urlAction, params)，需要自己去写接口、实现。
     * @param params      pageService.getPageConditional(pn, ecp, urlAction, params)最后的参数
     * @param <T>
     * @throws ServletException
     * @throws IOException
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static <T> void page(HttpServletRequest request, HttpServletResponse response, String urlAction, String url, PageService<T> pageService, Method getPage, Object... params) throws ServletException, IOException, SQLException, InvocationTargetException, IllegalAccessException {
        int pn = DataConverters.parseInt(request.getParameter("pn"), 1);
        int ecp = DataConverters.parseInt(request.getParameter("ecp"), Page.ENTRY_COUNT_PER_PAGE);
        Object[] params1 = {pn, ecp, urlAction};
        params1 = DataProcessUtils.combineArrays(params1, params);
        Page<T> page = (Page<T>) getPage.invoke(pageService, params1);
        request.setAttribute("page", page);
        request.getRequestDispatcher(url).forward(request, response);
    }
}
